 package com.manlost.web.common.cookie;

import javax.servlet.http.HttpServletRequest;

import com.manlost.biz.account.model.dataobject.Account;

/**
 * ManLostCookieManger工具类
 *
 * @author kang.zhouk
 */
public class ManLostCookieSystemUtils {

	public static ManLostCookieManger getManager(HttpServletRequest request) {
		return (ManLostCookieManger)request.getAttribute(ManLostCookieManger.MANLOST_COOKIE_MANGER);
	}
	
	/**
	 * login 写cookie 暂时放在这里
	 *
	 * @param request 
	 * 
	 * @param cookieManager
	 * @param account
	 */
	public static void setManLostCookieForLogin(HttpServletRequest request, Account account,boolean isAutoSign) {
		
		ManLostCookieManger cookieManager = getManager(request);
		
		//misc_t
		cookieManager.setCookie(MLostCookieNameEnum.MLSC_TEMP, MLostCookieKeyEnum.ACCOUNT_EMAIL, account.getEmail());
		cookieManager.setCookie(MLostCookieNameEnum.MLSC_TEMP, MLostCookieKeyEnum.ACCOUNT_ID, Long.toString(account.getId()));
		cookieManager.setCookie(MLostCookieNameEnum.MLSC_TEMP, MLostCookieKeyEnum.ACCOUNT_NAME, account.getUsername());
		cookieManager.setCookie(MLostCookieNameEnum.MLSC_TEMP, MLostCookieKeyEnum.IS_SIGN, "y");
		
		//misc_f
		cookieManager.setCookie(MLostCookieNameEnum.MLSC_FOREVER, MLostCookieKeyEnum.AUTO_SIGN, "n");
		cookieManager.setCookie(MLostCookieNameEnum.MLSC_FOREVER, MLostCookieKeyEnum.LAST_LOGIN_TIME, Long.toString(System.currentTimeMillis()));
		
		if(isAutoSign) {
			cookieManager.setCookie(MLostCookieNameEnum.MLSC_FOREVER, MLostCookieKeyEnum.ACCOUNT_EMAIL, account.getEmail());
			cookieManager.setCookie(MLostCookieNameEnum.MLSC_FOREVER, MLostCookieKeyEnum.ACCOUNT_ID, Long.toString(account.getId()));
			cookieManager.setCookie(MLostCookieNameEnum.MLSC_FOREVER, MLostCookieKeyEnum.ACCOUNT_NAME, account.getUsername());
			cookieManager.setCookie(MLostCookieNameEnum.MLSC_FOREVER, MLostCookieKeyEnum.AUTO_SIGN, "y");
		}
		cookieManager.saveAllCookie();
	}
	
	/**
	 * logout 清理cookie
	 *
	 * @param request 
	 * 
	 * @param cookieManager
	 * @param account
	 */
	public static void setManLostCookieForLogin(HttpServletRequest request) {
		
		ManLostCookieManger cookieManager = getManager(request);
		cookieManager.clearAllCookie();
		cookieManager.saveAllCookie();
	}
}
